<?php
require '../script/mysqlconfig.php';
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// 检查连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// 检查是否有文件上传
if ($_FILES["csvfile"]["error"] == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["csvfile"]["tmp_name"];
//    $name = basename($_FILES["csvfile"]["name"]);
//    echo $tmp_name . "___________" . $name . "<br>";

    // 打开上传的CSV文件
    if (($handle = fopen($tmp_name, "r")) !== FALSE) {
        // 跳过CSV文件的标题行
        fgetcsv($handle, 1000, ",");

        // 逐行读取CSV文件
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//            $num=count($data); //计算数组元素个数
            //iconv函数解析乱码字符，转移utf8字符解析，$conn->real_escape_string解析需要转义的字符，例如“ /
            $brand = iconv('gb2312','utf-8', $conn->real_escape_string($data[0]));
            $model = iconv('gb2312','utf-8', $conn->real_escape_string($data[1]));
            $serial_number = iconv('gb2312','utf-8', $conn->real_escape_string($data[2]));
            $device_type = iconv('gb2312','utf-8', $conn->real_escape_string($data[3]));
            $room = iconv('gb2312','utf-8', $conn->real_escape_string($data[4]));
            $cabinet = iconv('gb2312','utf-8', $conn->real_escape_string($data[5]));
            $start_u = iconv('gb2312','utf-8', $conn->real_escape_string($data[6]));
            $end_u = iconv('gb2312','utf-8', $conn->real_escape_string($data[7]));
            $device_status = iconv('gb2312','utf-8', $conn->real_escape_string($data[8]));
            $device_feature = iconv('gb2312','utf-8', $conn->real_escape_string($data[9]));
            $bmc_ip = iconv('gb2312','utf-8', $conn->real_escape_string($data[10]));
            $manage_ip = iconv('gb2312','utf-8', $conn->real_escape_string($data[11]));
            $yewu_ip = iconv('gb2312','utf-8', $conn->real_escape_string($data[12]));
            $device_belong = iconv('gb2312','utf-8', $conn->real_escape_string($data[13]));
            $belong_en = iconv('gb2312','utf-8', $conn->real_escape_string($data[14]));
            $belong_op = iconv('gb2312','utf-8', $conn->real_escape_string($data[15]));
            $asset_code = iconv('gb2312','utf-8', $conn->real_escape_string($data[16]));
            $use_en = iconv('gb2312','utf-8', $conn->real_escape_string($data[17]));
            $use_operator = iconv('gb2312','utf-8', $conn->real_escape_string($data[18]));
            $client_no = iconv('gb2312','utf-8', $conn->real_escape_string($data[19]));
            $client_name = iconv('gb2312','utf-8', $conn->real_escape_string($data[20]));
            $other = iconv('gb2312','utf-8', $conn->real_escape_string($data[21]));

            // 插入数据到数据库
            $sql = "INSERT INTO hosts (brand, model, serial_number, device_type, room, cabinet, start_u, end_u, device_status, device_feature, bmc_ip, manage_ip, yewu_ip, device_belong, belong_en, belong_op, asset_code, use_en, use_operator, client_no,  client_name, other) VALUES ('$brand', '$model', '$serial_number', '$device_type','$room','$cabinet','$start_u','$end_u','$device_status','$device_feature','$bmc_ip','$manage_ip','$yewu_ip','$device_belong','$belong_en','$belong_op','$asset_code','$use_en','$use_operator','$client_no','$client_name', '$other')";
            if ($conn->query($sql) !== TRUE) {
                echo $brand ." ". $model . " ". $serial_number. "导入失败:" . $sql . "<br>" . $conn->error . "<br>";
            } else{
                echo $brand ." ". $model . " ". $serial_number. "导入成功。". "<br>";
            }
        }
        fclose($handle);
    } else {
        echo "无法打开CSV文件。";
    }
} else {
    echo "CSV文件数据上传出错。";
}

// 关闭数据库连接
$conn->close();
?>

